package com.gdzn.signal.controller;


import cn.hutool.core.bean.BeanUtil;
import com.gdzn.common.core.domain.R;
import com.gdzn.signal.mongo.document.MgSignalAlarm;
import com.gdzn.signal.mongo.service.IMgSignalAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mongodb 操作测试
 */
@RestController
@RequestMapping("/crud" )
public class DemoMgCurdController {
    @Autowired
    private IMgSignalAlarmService mgSignalAlarmService;

    @PostMapping("/insert")
    public R insert(@RequestBody MgSignalAlarm alarm) {
        MgSignalAlarm alarm2 = mgSignalAlarmService.insert(alarm);
        return R.ok(alarm2);
    }
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id" ) String id) {
        MgSignalAlarm alarm2 = mgSignalAlarmService.findById(id);
        return R.ok(alarm2);
    }

    @GetMapping("/findAll")
    public R findByAll() {
        List<MgSignalAlarm> alarm2 = mgSignalAlarmService.findAll();
        return R.ok(alarm2);
    }
    @PostMapping("/findByParam")
    public R findByParam(@RequestBody MgSignalAlarm alarm) {
        List<MgSignalAlarm> alarm2 = mgSignalAlarmService.findAll(alarm);
        return R.ok(alarm2);
    }

    @PostMapping("/findOne")
    public R findOne(@RequestBody MgSignalAlarm alarm) {
        MgSignalAlarm alarm2 = mgSignalAlarmService.findOne(alarm);
        return R.ok(alarm2);
    }
    @PostMapping("/findListPage")
    public R findListPage(@RequestBody MgSignalAlarm alarm) {
        Page<MgSignalAlarm>  alarm2 = mgSignalAlarmService.findListPage(alarm,1,5);
        return R.ok(alarm2);
    }

    @PostMapping("/modifyById")
    public R modify(@RequestBody MgSignalAlarm alarm) {
        MgSignalAlarm alarm2 = mgSignalAlarmService.update(alarm);
        return R.ok(alarm2);
    }

    @PostMapping("/updateById")
    public R updateById(@RequestBody MgSignalAlarm alarm) {
        Map<String, Object> map = BeanUtil.beanToMap(alarm);
        mgSignalAlarmService.updateById(alarm.getId(),map);
        return R.ok(alarm);
    }

    @PostMapping("/updateByParam")
    public R updateByParam(@RequestBody MgSignalAlarm alarm) {
        Map<String, Object> map = BeanUtil.beanToMap(alarm);
        Map<String, Object> updatemap = new HashMap<>();
        updatemap.put("id",alarm.getId());
        mgSignalAlarmService.updateByParam(updatemap,map);
        return R.ok(alarm);
    }
    @GetMapping("/deleted/{id}")
    public R deleted(@PathVariable("id") String id) {
        mgSignalAlarmService.deleteById(id);
        return R.ok();
    }
    @GetMapping("/deleteByIds/{ids}")
    public R deleteByIds(@PathVariable("ids") String[] ids) {
        mgSignalAlarmService.deleteByIds(ids);
        return R.ok();
    }

}
